
# ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
# DESCRIPTION
# ______________________________________________________________________________

# This code creates Figures 1, 2, A.1, and C.7.

# Preamble ---------------------------------------------------------------------

rm(list = ls())

# Packages ---------------------------------------------------------------------

library(tidyverse)
library(ggpubr)
library(ggh4x)
library(ggtext)
library(haven)
library(patchwork)
library(rstatix)
library(scales)

options(scipen = 999)
set.seed(999)

# Bar Plot ---------------------------------------------------------------------

data <- read_dta("data/data.dta")

data2 <- data %>%
  select(secessionistconflict,
         frontline,
         lead,
         noncombat,
         participation) %>%
  pivot_longer(cols = c(frontline, lead, noncombat, participation),
                      names_to = "role") %>%
  drop_na()

data3 <- data2 %>%
  group_by(secessionistconflict) %>%
  add_count(name = "n_secessionistconflict") %>%
  group_by(secessionistconflict, role) %>%
  add_count(name = "n_role") %>%
  add_count(value == 1) %>%
  distinct() %>%
  mutate(prop = n / n_role) %>%
  ungroup() %>%
  mutate(
    role = case_when(
      role == "frontline" ~  "Front-Line Combat",
      role == "lead" ~  "Leadership",
      role == "noncombat" ~  "Non-Combat",
      role == "participation" ~  "Any"
    ),
    role = factor(
      role,
      levels = c("Front-Line Combat", "Leadership", "Non-Combat", "Any")
    )
  )

data3 <- data3 %>%
  mutate(top_panel = "Secessionism and Women’s Participation in Rebel Groups")

stat.test <- data3 %>%
  uncount(weights = n) %>%
  group_by(role) %>%
  t_test(value ~ secessionistconflict) %>%
  mutate(label = case_when(
    p < 0.001 ~ "****",
    p < 0.01 ~ "***",
    p < 0.05 ~ "**",
    p < 0.1 ~ "*",
    p >= 0.1 ~ "ns"
  ))

data3 %>%
  filter(value == 1) %>%
  mutate(secessionistconflict = 
           factor(secessionistconflict, levels = c("1", "0"))) %>%
  ggbarplot(
    x = "role",
    y = "prop",
    fill = "secessionistconflict",
    position =  position_dodge(width = 0.8)
  ) +
  geom_text(
    aes(
      label = percent(prop, accuracy = 1),
      y = prop + 0.01,
      x = role,
      group = secessionistconflict
    ),
    position = position_dodge(width = 0.8),
    vjust = 0
  ) +
  annotate(
    "segment",
    x = c(1, 2, 3, 4) - 0.2,
    xend = c(1, 2, 3, 4) + 0.2,
    y = c(
      0.89836066 + 0.07,
      0.76065574 + 0.07,
      0.95409836 + 0.07,
      0.96721311 + 0.07
    ),
    yend = c(
      0.89836066 + 0.07,
      0.76065574 + 0.07,
      0.95409836 + 0.07,
      0.96721311 + 0.07
    ),
    lineend = "round"
  ) +
  annotate(
    "segment",
    x = sort(c((c(
      1, 2, 3, 4
    ) - 0.2), c(1, 2, 3, 4) + 0.2)),
    xend = sort(c((c(
      1, 2, 3, 4
    ) - 0.2), c(1, 2, 3, 4) + 0.2)),
    y = rep(
      c(
        0.89836066 + 0.07,
        0.76065574 + 0.07,
        0.95409836 + 0.07,
        0.96721311 + 0.07
      ),
      each = 2
    ),
    yend = rep(
      c(
        0.89836066 + 0.07,
        0.76065574 + 0.07,
        0.95409836 + 0.07,
        0.96721311 + 0.07
      ) - 0.02,
      each = 2
    ),
    lineend = "round"
  ) +
  scale_y_continuous(
    expand = expansion(mult = c(0, 0.05)),
    breaks = seq(0, 1, 0.25),
    labels = label_percent()
  ) +
  scale_fill_manual(
    values = c("0" = "grey", "1" = "#d4a3ff"),
    labels = c("0" = "Non-Secessionist", "1" = "Secessionist"),
  ) +
  facet_wrap( ~ top_panel, ncol = 1, strip.position = "top") +
  labs(x = "Women's Role in Rebel Groups", 
       y = "Proportion of Rebel Groups with Women's Participation (%)", 
       fill = "Civil Conflict Type") +
  theme_classic() +
  theme(
    panel.border = element_rect(fill = NA, color = "black"),
    strip.background = element_rect(
      linewidth = 0.5,
      fill = "white",
      color = "black"
    ),
    strip.text = element_text(size = 12, margin = margin(t = 7.5, b = 7.5)),
    text = element_text(size = 12),
    panel.grid.major.y = element_line(),
    axis.text = element_text(color = "black"),
    axis.ticks = element_line(color = "black"),
    axis.ticks.x = element_blank(),
    legend.position = "bottom",
    legend.margin = margin(-5, 0, 0, 0, "pt"),
    plot.margin = margin(3, 3, 3, 3, "pt")
  ) +
  stat_pvalue_manual(
    stat.test,
    x = "role",
    y.position = c(
      0.89836066 + 0.07,
      0.76065574 + 0.07,
      0.95409836 + 0.07,
      0.96721311 + 0.07
    ) + 0.01,
    tip.length = 0.01,
    remove.bracket = FALSE,
    label = "label",
    position = position_dodge(0.8)
  ) +
  coord_cartesian(ylim = c(0, 1.04), clip = "off")

ggsave(
  "figures/summary plots/bar_plot.pdf",
  device = cairo_pdf,
  width = 6,
  height = 5.5
)

# Summary Plots ----------------------------------------------------------------

add_global_label <-
  function(pwobj,
           Xlab = NULL,
           Ylab = NULL,
           Xgap = 0.03,
           Ygap = 0.03,
           ...) {
    ylabgrob <- plot_spacer()
    if (!is.null(Ylab)) {
      ylabgrob <- ggplot() +
        geom_text(aes(x = .5, y = .5), label = Ylab, angle = 90, ...) +
        theme_void()
    }
    if (!is.null(Xlab)) {
      xlabgrob <- ggplot() +
        geom_text(aes(x = .5, y = .5), label = Xlab, ...) +
        theme_void()
    }
    if (!is.null(Ylab) & is.null(Xlab)) {
      return((ylabgrob + patchworkGrob(pwobj)) +
               plot_layout(widths = 100 * c(Ygap, 1 - Ygap)))
    }
    if (is.null(Ylab) & !is.null(Xlab)) {
      return((ylabgrob + pwobj) +
               (xlabgrob) +
               plot_layout(
                 heights = 100 * c(1 - Xgap, Xgap),
                 widths = c(0, 100),
                 design = "
                                   AB
                                   CC
                                   "
               )
      )
    }
    if (!is.null(Ylab) & !is.null(Xlab)) {
      return((ylabgrob + pwobj) +
               (xlabgrob) +
               plot_layout(
                 heights = 100 * c(1 - Xgap, Xgap),
                 widths = 100 * c(Ygap, 1 - Ygap),
                 design = "
                                   AB
                                   CC
                                   "
               )
      )
    }
    return(pwobj)
  }

## Max -------------------------------------------------------------------------

data <- read_dta("data/data.dta")

data$rebelsv_max[is.na(data$rebelsv_max)] <- "bla"
data$statesv_max[is.na(data$statesv_max)] <- "bla"
label_prev <- c("0", "1", "2", "3")

violin_rebel <- data %>%
  ggplot(aes(x = factor(rebelsv_max))) +
  geom_violin(aes(y = year, fill = factor(rebelsv_max)),
              scale = "width",
              linewidth = 0.5) +
  geom_jitter(
    aes(y = year),
    alpha = 0.2,
    color = "black",
    fill = NA,
    shape = 1,
    width = 0.15
  ) +
  geom_boxplot(
    aes(y = year),
    color = "#f2f2f2",
    fill = NA,
    alpha = 0.8,
    width = 0.3,
    outlier.shape = NA
  ) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  scale_x_discrete(labels = label_prev) +
  facet_wrap2("Rebel SV" ~ .,
              strip = strip_themed(background_x =
                                     elem_list_rect(aes(fill = rebelsv_max))),
              axes = "x") +
  labs(y = "Year") +
  theme_bw() +
  theme(
    panel.border = element_rect(fill = NA, color = "black"),
    strip.background = element_rect(
      linewidth = 0.5,
      fill = "white",
      color = "black"
    ),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_text(size = 15, margin = margin(t = 7.5, b = 7.5)),
    legend.position = "none",
    axis.title.x = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    axis.text.x = element_blank(),
    plot.title = element_text(hjust = 0.5, size = 20),
    text = element_text(size = 18),
    axis.title.y = element_text(margin = margin(
      t = 0,
      r = 10,
      b = 0,
      l = 0
    ))
  ) +
  coord_cartesian(clip = "off")

bar_rebel <- data %>%
  ggplot(aes(x = factor(rebelsv_max))) +
  geom_bar(
    aes(fill = factor(rebelsv_max)),
    linewidth = 0.5,
    width = 0.8,
    color = "black",
    show.legend = FALSE
  ) +
  geom_richtext(
    stat = "count",
    size = 5.5,
    vjust = 0.1,
    lineheight = 0.8,
    fill = NA,
    label.color = NA,
    aes(
      label = paste0(
        "<span style='font-size:13pt'>",
        percent(after_stat(count) / sum(after_stat(count)), accuracy = 1),
        "</span>",
        "<br>",
        after_stat(count)
      )
    )
  ) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 1100)) +
  scale_x_discrete(labels = label_prev) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  facet_wrap2("Rebel SV" ~ .,
              strip = strip_themed(background_x =
                                     elem_list_rect(aes(fill = rebelsv_max))),
              axes = "x") +
  labs(y = "Count") +
  theme_bw() +
  theme(
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    panel.border = element_rect(fill = NA, color = "black"),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_blank(),
    axis.title.x = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    text = element_text(size = 18)
  ) +
  coord_cartesian(clip = "off")

violin_state <- data %>%
  ggplot(aes(x = factor(statesv_max))) +
  geom_violin(aes(y = year, fill = factor(statesv_max)),
              scale = "width",
              linewidth = 0.5) +
  geom_jitter(
    aes(y = year),
    alpha = 0.2,
    color = "black",
    fill = NA,
    shape = 1,
    width = 0.15
  ) +
  geom_boxplot(
    aes(y = year),
    color = "#f2f2f2",
    fill = NA,
    alpha = 0.8,
    width = 0.3,
    outlier.shape = NA
  ) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  scale_x_discrete(labels = label_prev) +
  facet_wrap2("State SV" ~ .,
              strip = strip_themed(background_x =
                                     elem_list_rect(aes(fill = statesv_max))),
              axes = "x") +
  labs(y = "Year") +
  theme_bw() +
  theme(
    panel.border = element_rect(fill = NA, color = "black"),
    strip.background = element_rect(
      linewidth = 0.5,
      fill = "white",
      color = "black"
    ),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_text(size = 15, margin = margin(t = 7.5, b = 7.5)),
    legend.position = "none",
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    axis.text.x = element_blank(),
    plot.title = element_text(hjust = 0.5, size = 20),
    text = element_text(size = 18)
  ) +
  coord_cartesian(clip = "off")

bar_state <- data %>%
  ggplot(aes(x = factor(statesv_max))) +
  geom_bar(
    aes(fill = factor(statesv_max)),
    linewidth = 0.5,
    width = 0.8,
    color = "black",
    show.legend = FALSE
  ) +
  geom_richtext(
    stat = "count",
    size = 5.5,
    vjust = 0.1,
    lineheight = 0.8,
    fill = NA,
    label.color = NA,
    aes(
      label = paste0(
        "<span style='font-size:13pt'>",
        percent(after_stat(count) / sum(after_stat(count)), accuracy = 1),
        "</span>",
        "<br>",
        after_stat(count)
      )
    )
  ) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 1100)) +
  scale_x_discrete(labels = label_prev) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  facet_wrap2("State SV" ~ .,
              strip = strip_themed(background_x =
                                     elem_list_rect(aes(fill = statesv_max))),
              axes = "x") +
  labs(y = "Count") +
  theme_bw() +
  theme(
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    panel.border = element_rect(fill = NA, color = "black"),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    text = element_text(size = 18),
  ) +
  coord_cartesian(clip = "off")

MAX <- ((violin_rebel / bar_rebel) | (violin_state / bar_state)) %>%
  add_global_label(
    Xlab = "Prevalence Level",
    size = 6,
    Xgap = 0.05,
    hjust = 0.2375,
    vjust = 0.1
  ) &
  theme(plot.margin = margin(3, 3, -7, 0.75, "pt"))

ggsave("figures/summary plots/summary_plot.pdf",
       width = 8,
       height = 8)

## US DoS ----------------------------------------------------------------------

data <- read_dta("data/data.dta")

label_prev <- c("0", "1", "2", "3")

violin_rebel <- data %>%
  filter(!is.na(rebelsv_usdos)) %>%
  ggplot(aes(x = factor(rebelsv_usdos))) +
  geom_violin(aes(y = year, fill = factor(rebelsv_usdos)),
              scale = "width",
              linewidth = 0.5) +
  geom_jitter(
    aes(y = year),
    alpha = 0.2,
    color = "black",
    fill = NA,
    shape = 1,
    width = 0.15
  ) +
  geom_boxplot(
    aes(y = year),
    color = "#f2f2f2",
    fill = NA,
    alpha = 0.8,
    width = 0.3,
    outlier.shape = NA
  ) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  scale_x_discrete(labels = label_prev) +
  facet_wrap2(
    "Rebel SV (US DoS)" ~ .,
    strip = strip_themed(background_x =
                           elem_list_rect(aes(fill = rebelsv_usdos))),
    axes = "x"
  ) +
  labs(y = "Year") +
  theme_bw() +
  theme(
    panel.border = element_rect(fill = NA, color = "black"),
    strip.background = element_rect(
      linewidth = 0.5,
      fill = "white",
      color = "black"
    ),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_text(size = 15, margin = margin(t = 7.5, b = 7.5)),
    legend.position = "none",
    axis.title.x = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    axis.text.x = element_blank(),
    plot.title = element_text(hjust = 0.5, size = 20),
    text = element_text(size = 18),
    axis.title.y = element_text(margin = margin(
      t = 0,
      r = 10,
      b = 0,
      l = 0
    ))
  ) +
  coord_cartesian(clip = "off")

bar_rebel <- data %>%
  filter(!is.na(rebelsv_usdos)) %>%
  ggplot(aes(x = factor(rebelsv_usdos))) +
  geom_bar(
    aes(fill = factor(rebelsv_usdos)),
    linewidth = 0.5,
    width = 0.8,
    color = "black",
    show.legend = FALSE
  ) +
  geom_richtext(
    stat = "count",
    size = 5.5,
    vjust = 0.1,
    lineheight = 0.8,
    fill = NA,
    label.color = NA,
    aes(
      label = paste0(
        "<span style='font-size:13pt'>",
        percent(after_stat(count) / sum(after_stat(count)), accuracy = 1),
        "</span>",
        "<br>",
        after_stat(count)
      )
    )
  ) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 1100)) +
  scale_x_discrete(labels = label_prev) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  facet_wrap2(
    "Rebel SV (US DoS)" ~ .,
    strip = strip_themed(background_x =
                           elem_list_rect(aes(fill = rebelsv_usdos))),
    axes = "x"
  ) +
  labs(y = "Count") +
  theme_bw() +
  theme(
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    panel.border = element_rect(fill = NA, color = "black"),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_blank(),
    axis.title.x = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    text = element_text(size = 18)
  ) +
  coord_cartesian(clip = "off")

violin_state <- data %>%
  filter(!is.na(statesv_usdos)) %>%
  ggplot(aes(x = factor(statesv_usdos))) +
  geom_violin(aes(y = year, fill = factor(statesv_usdos)),
              scale = "width",
              linewidth = 0.5) +
  geom_jitter(
    aes(y = year),
    alpha = 0.2,
    color = "black",
    fill = NA,
    shape = 1,
    width = 0.15
  ) +
  geom_boxplot(
    aes(y = year),
    color = "#f2f2f2",
    fill = NA,
    alpha = 0.8,
    width = 0.3,
    outlier.shape = NA
  ) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  scale_x_discrete(labels = label_prev) +
  facet_wrap2(
    "State SV (US DoS)" ~ .,
    strip = strip_themed(background_x =
                           elem_list_rect(aes(fill = statesv_usdos))),
    axes = "x"
  ) +
  labs(y = "Year") +
  theme_bw() +
  theme(
    panel.border = element_rect(fill = NA, color = "black"),
    strip.background = element_rect(
      linewidth = 0.5,
      fill = "white",
      color = "black"
    ),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_text(size = 15, margin = margin(t = 7.5, b = 7.5)),
    legend.position = "none",
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    axis.text.x = element_blank(),
    plot.title = element_text(hjust = 0.5, size = 20),
    text = element_text(size = 18)
  ) +
  coord_cartesian(clip = "off")

bar_state <- data %>%
  filter(!is.na(statesv_usdos)) %>%
  ggplot(aes(x = factor(statesv_usdos))) +
  geom_bar(
    aes(fill = factor(statesv_usdos)),
    linewidth = 0.5,
    width = 0.8,
    color = "black",
    show.legend = FALSE
  ) +
  geom_richtext(
    stat = "count",
    size = 5.5,
    vjust = 0.1,
    lineheight = 0.8,
    fill = NA,
    label.color = NA,
    aes(
      label = paste0(
        "<span style='font-size:13pt'>",
        percent(after_stat(count) / sum(after_stat(count)), accuracy = 1),
        "</span>",
        "<br>",
        after_stat(count)
      )
    )
  ) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 1100)) +
  scale_x_discrete(labels = label_prev) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  facet_wrap2(
    "State SV (US DoS)" ~ .,
    strip = strip_themed(background_x =
                           elem_list_rect(aes(fill = statesv_usdos))),
    axes = "x"
  ) +
  labs(y = "Count") +
  theme_bw() +
  theme(
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    panel.border = element_rect(fill = NA, color = "black"),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    text = element_text(size = 18),
  ) +
  coord_cartesian(clip = "off")

USDOS <- ((violin_rebel / bar_rebel) |
            (violin_state / bar_state)) %>%
  add_global_label(
    Xlab = "Prevalence Level",
    size = 6,
    Xgap = 0.05,
    hjust = 0.2375,
    vjust = 0.1
  ) &
  theme(plot.margin = margin(3, 3, -7, 0.75, "pt"))

## AI --------------------------------------------------------------------------

data <- read_dta("data/data.dta")

label_prev <- c("0", "1", "2", "3")

violin_rebel <- data %>%
  filter(!is.na(rebelsv_ai)) %>%
  ggplot(aes(x = factor(rebelsv_ai))) +
  geom_violin(aes(y = year, fill = factor(rebelsv_ai)),
              scale = "width",
              linewidth = 0.5) +
  geom_jitter(
    aes(y = year),
    alpha = 0.2,
    color = "black",
    fill = NA,
    shape = 1,
    width = 0.15
  ) +
  geom_boxplot(
    aes(y = year),
    color = "#f2f2f2",
    fill = NA,
    alpha = 0.8,
    width = 0.3,
    outlier.shape = NA
  ) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  scale_x_discrete(labels = label_prev) +
  facet_wrap2("Rebel SV (AI)" ~ .,
              strip = strip_themed(background_x =
                                     elem_list_rect(aes(fill = rebelsv_ai))),
              axes = "x") +
  labs(y = "Year") +
  theme_bw() +
  theme(
    panel.border = element_rect(fill = NA, color = "black"),
    strip.background = element_rect(
      linewidth = 0.5,
      fill = "white",
      color = "black"
    ),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_text(size = 15, margin = margin(t = 7.5, b = 7.5)),
    legend.position = "none",
    axis.title.x = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    axis.text.x = element_blank(),
    plot.title = element_text(hjust = 0.5, size = 20),
    text = element_text(size = 18),
    axis.title.y = element_text(margin = margin(
      t = 0,
      r = 10,
      b = 0,
      l = 0
    ))
  ) +
  coord_cartesian(clip = "off")

bar_rebel <- data %>%
  filter(!is.na(rebelsv_ai)) %>%
  ggplot(aes(x = factor(rebelsv_ai))) +
  geom_bar(
    aes(fill = factor(rebelsv_ai)),
    linewidth = 0.5,
    width = 0.8,
    color = "black",
    show.legend = FALSE
  ) +
  geom_richtext(
    stat = "count",
    size = 5.5,
    vjust = 0.1,
    lineheight = 0.8,
    fill = NA,
    label.color = NA,
    aes(
      label = paste0(
        "<span style='font-size:13pt'>",
        percent(after_stat(count) / sum(after_stat(count)), accuracy = 1),
        "</span>",
        "<br>",
        after_stat(count)
      )
    )
  ) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 1100)) +
  scale_x_discrete(labels = label_prev) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  facet_wrap2("Rebel SV (AI)" ~ .,
              strip = strip_themed(background_x =
                                     elem_list_rect(aes(fill = rebelsv_ai))),
              axes = "x") +
  labs(y = "Count") +
  theme_bw() +
  theme(
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    panel.border = element_rect(fill = NA, color = "black"),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_blank(),
    axis.title.x = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    text = element_text(size = 18)
  ) +
  coord_cartesian(clip = "off")

violin_state <- data %>%
  filter(!is.na(statesv_ai)) %>%
  ggplot(aes(x = factor(statesv_ai))) +
  geom_violin(aes(y = year, fill = factor(statesv_ai)),
              scale = "width",
              linewidth = 0.5) +
  geom_jitter(
    aes(y = year),
    alpha = 0.2,
    color = "black",
    fill = NA,
    shape = 1,
    width = 0.15
  ) +
  geom_boxplot(
    aes(y = year),
    color = "#f2f2f2",
    fill = NA,
    alpha = 0.8,
    width = 0.3,
    outlier.shape = NA
  ) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  scale_x_discrete(labels = label_prev) +
  facet_wrap2("State SV (AI)" ~ .,
              strip = strip_themed(background_x =
                                     elem_list_rect(aes(fill = statesv_ai))),
              axes = "x") +
  labs(y = "Year") +
  theme_bw() +
  theme(
    panel.border = element_rect(fill = NA, color = "black"),
    strip.background = element_rect(
      linewidth = 0.5,
      fill = "white",
      color = "black"
    ),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_text(size = 15, margin = margin(t = 7.5, b = 7.5)),
    legend.position = "none",
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    axis.text.x = element_blank(),
    plot.title = element_text(hjust = 0.5, size = 20),
    text = element_text(size = 18)
  ) +
  coord_cartesian(clip = "off")

bar_state <- data %>%
  filter(!is.na(statesv_ai)) %>%
  ggplot(aes(x = factor(statesv_ai))) +
  geom_bar(
    aes(fill = factor(statesv_ai)),
    linewidth = 0.5,
    width = 0.8,
    color = "black",
    show.legend = FALSE
  ) +
  geom_richtext(
    stat = "count",
    size = 5.5,
    vjust = 0.1,
    lineheight = 0.8,
    fill = NA,
    label.color = NA,
    aes(
      label = paste0(
        "<span style='font-size:13pt'>",
        percent(after_stat(count) / sum(after_stat(count)), accuracy = 1),
        "</span>",
        "<br>",
        after_stat(count)
      )
    )
  ) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 1100)) +
  scale_x_discrete(labels = label_prev) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  facet_wrap2("State SV (AI)" ~ .,
              strip = strip_themed(background_x =
                                     elem_list_rect(aes(fill = statesv_ai))),
              axes = "x") +
  labs(y = "Count") +
  theme_bw() +
  theme(
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    panel.border = element_rect(fill = NA, color = "black"),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    text = element_text(size = 18),
  ) +
  coord_cartesian(clip = "off")

AI <- ((violin_rebel / bar_rebel) | (violin_state / bar_state)) %>%
  add_global_label(
    Xlab = "Prevalence Level",
    size = 6,
    Xgap = 0.05,
    hjust = 0.2375,
    vjust = 0.1
  ) &
  theme(plot.margin = margin(3, 3, -7, 0.75, "pt"))

## HRW -------------------------------------------------------------------------

data <- read_dta("data/data.dta")

label_prev <- c("0", "1", "2", "3")

violin_rebel <- data %>%
  filter(!is.na(rebelsv_hrw)) %>%
  ggplot(aes(x = factor(rebelsv_hrw))) +
  geom_violin(aes(y = year, fill = factor(rebelsv_hrw)),
              scale = "width",
              linewidth = 0.5) +
  geom_jitter(
    aes(y = year),
    alpha = 0.2,
    color = "black",
    fill = NA,
    shape = 1,
    width = 0.15
  ) +
  geom_boxplot(
    aes(y = year),
    color = "#f2f2f2",
    fill = NA,
    alpha = 0.8,
    width = 0.3,
    outlier.shape = NA
  ) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  scale_x_discrete(labels = label_prev) +
  facet_wrap2(
    "Rebel SV (HRW)" ~ .,
    strip = strip_themed(background_x =
                           elem_list_rect(aes(fill = rebelsv_hrw))),
    axes = "x"
  ) +
  labs(y = "Year") +
  theme_bw() +
  theme(
    panel.border = element_rect(fill = NA, color = "black"),
    strip.background = element_rect(
      linewidth = 0.5,
      fill = "white",
      color = "black"
    ),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_text(size = 15, margin = margin(t = 7.5, b = 7.5)),
    legend.position = "none",
    axis.title.x = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    axis.text.x = element_blank(),
    plot.title = element_text(hjust = 0.5, size = 20),
    text = element_text(size = 18),
    axis.title.y = element_text(margin = margin(
      t = 0,
      r = 10,
      b = 0,
      l = 0
    ))
  ) +
  coord_cartesian(clip = "off")

bar_rebel <- data %>%
  filter(!is.na(rebelsv_hrw)) %>%
  ggplot(aes(x = factor(rebelsv_hrw))) +
  geom_bar(
    aes(fill = factor(rebelsv_hrw)),
    linewidth = 0.5,
    width = 0.8,
    color = "black",
    show.legend = FALSE
  ) +
  geom_richtext(
    stat = "count",
    size = 5.5,
    vjust = 0.1,
    lineheight = 0.8,
    fill = NA,
    label.color = NA,
    aes(
      label = paste0(
        "<span style='font-size:13pt'>",
        percent(after_stat(count) / sum(after_stat(count)), accuracy = 1),
        "</span>",
        "<br>",
        after_stat(count)
      )
    )
  ) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 1100)) +
  scale_x_discrete(labels = label_prev) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  facet_wrap2(
    "Rebel SV (HRW)" ~ .,
    strip = strip_themed(background_x =
                           elem_list_rect(aes(fill = rebelsv_hrw))),
    axes = "x"
  ) +
  labs(y = "Count") +
  theme_bw() +
  theme(
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    panel.border = element_rect(fill = NA, color = "black"),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_blank(),
    axis.title.x = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    text = element_text(size = 18)
  ) +
  coord_cartesian(clip = "off")

violin_state <- data %>%
  filter(!is.na(statesv_hrw)) %>%
  ggplot(aes(x = factor(statesv_hrw))) +
  geom_violin(aes(y = year, fill = factor(statesv_hrw)),
              scale = "width",
              linewidth = 0.5) +
  geom_jitter(
    aes(y = year),
    alpha = 0.2,
    color = "black",
    fill = NA,
    shape = 1,
    width = 0.15
  ) +
  geom_boxplot(
    aes(y = year),
    color = "#f2f2f2",
    fill = NA,
    alpha = 0.8,
    width = 0.3,
    outlier.shape = NA
  ) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  scale_x_discrete(labels = label_prev) +
  facet_wrap2(
    "State SV (HRW)" ~ .,
    strip = strip_themed(background_x =
                           elem_list_rect(aes(fill = statesv_hrw))),
    axes = "x"
  ) +
  labs(y = "Year") +
  theme_bw() +
  theme(
    panel.border = element_rect(fill = NA, color = "black"),
    strip.background = element_rect(
      linewidth = 0.5,
      fill = "white",
      color = "black"
    ),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_text(size = 15, margin = margin(t = 7.5, b = 7.5)),
    legend.position = "none",
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    axis.text.x = element_blank(),
    plot.title = element_text(hjust = 0.5, size = 20),
    text = element_text(size = 18)
  ) +
  coord_cartesian(clip = "off")

bar_state <- data %>%
  filter(!is.na(statesv_hrw)) %>%
  ggplot(aes(x = factor(statesv_hrw))) +
  geom_bar(
    aes(fill = factor(statesv_hrw)),
    linewidth = 0.5,
    width = 0.8,
    color = "black",
    show.legend = FALSE
  ) +
  geom_richtext(
    stat = "count",
    size = 5.5,
    vjust = 0.1,
    lineheight = 0.8,
    fill = NA,
    label.color = NA,
    aes(
      label = paste0(
        "<span style='font-size:13pt'>",
        percent(after_stat(count) / sum(after_stat(count)), accuracy = 1),
        "</span>",
        "<br>",
        after_stat(count)
      )
    )
  ) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 1100)) +
  scale_x_discrete(labels = label_prev) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  facet_wrap2(
    "State SV (HRW)" ~ .,
    strip = strip_themed(background_x =
                           elem_list_rect(aes(fill = statesv_hrw))),
    axes = "x"
  ) +
  labs(y = "Count") +
  theme_bw() +
  theme(
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    panel.border = element_rect(fill = NA, color = "black"),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    text = element_text(size = 18),
  ) +
  coord_cartesian(clip = "off")

HRW <- ((violin_rebel / bar_rebel) | (violin_state / bar_state)) %>%
  add_global_label(
    Xlab = "Prevalence Level",
    size = 6,
    Xgap = 0.05,
    hjust = 0.2375,
    vjust = 0.1
  ) &
  theme(plot.margin = margin(3, 3, -7, 0.75, "pt"))

## Source-Specific -------------------------------------------------------------

((MAX / AI) | (USDOS / HRW)) &
  theme(plot.margin = margin(3, 3, -5, 1, "pt"))

ggsave(
  "figures/summary plots/summary_plot_ss.pdf",
  width = 16,
  height = 16
)

## Cross-Sectional -------------------------------------------------------------

data_cs <- read_dta("data/data_cs.dta")

data_cs$rebelsv_max[is.na(data_cs$rebelsv_max)] <- "bla"
data_cs$statesv_max[is.na(data_cs$statesv_max)] <- "bla"
label_prev <- c("0", "1", "2", "3")

bar_rebel <- data_cs %>%
  ggplot(aes(x = factor(rebelsv_max))) +
  geom_bar(
    aes(fill = factor(rebelsv_max)),
    linewidth = 0.5,
    width = 0.8,
    color = "black",
    show.legend = FALSE
  ) +
  geom_richtext(
    stat = "count",
    size = 5.5,
    vjust = 0.1,
    lineheight = 0.8,
    fill = NA,
    label.color = NA,
    aes(
      label = paste0(
        "<span style='font-size:13pt'>",
        percent(after_stat(count) / sum(after_stat(count)), accuracy = 1),
        "</span>",
        "<br>",
        after_stat(count)
      )
    )
  ) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 210)) +
  scale_x_discrete(labels = label_prev) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  facet_wrap2("Rebel SV" ~ .,
              strip = strip_themed(background_x =
                                     elem_list_rect(aes(fill = rebelsv_max))),
              axes = "x") +
  labs(y = "Count") +
  theme_bw() +
  theme(
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    panel.border = element_rect(fill = NA, color = "black"),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_text(size = 15, margin = margin(t = 7.5, b = 7.5)),
    axis.title.x = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    text = element_text(size = 18)
  ) +
  coord_cartesian(clip = "off")

bar_state <- data_cs %>%
  ggplot(aes(x = factor(statesv_max))) +
  geom_bar(
    aes(fill = factor(statesv_max)),
    linewidth = 0.5,
    width = 0.8,
    color = "black",
    show.legend = FALSE
  ) +
  geom_richtext(
    stat = "count",
    size = 5.5,
    vjust = 0.1,
    lineheight = 0.8,
    fill = NA,
    label.color = NA,
    aes(
      label = paste0(
        "<span style='font-size:13pt'>",
        percent(after_stat(count) / sum(after_stat(count)), accuracy = 1),
        "</span>",
        "<br>",
        after_stat(count)
      )
    )
  ) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 210)) +
  scale_x_discrete(labels = label_prev) +
  scale_fill_manual(
    values = c(
      "0" = "#eef4ff",
      "1" = "#b8bed4",
      "2" = "#878ca1",
      "3" = "#585e71",
      "bla" = "#717171"
    )
  ) +
  facet_wrap2("State SV" ~ .,
              strip = strip_themed(background_x =
                                     elem_list_rect(aes(fill = statesv_max))),
              axes = "x") +
  labs(y = "Count") +
  theme_bw() +
  theme(
    strip.background = element_rect(linewidth = 0.5, fill = "white"),
    panel.border = element_rect(fill = NA, color = "black"),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    strip.text = element_text(size = 15, margin = margin(t = 7.5, b = 7.5)),
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(color = "black"),
    axis.text = element_text(color = "black"),
    text = element_text(size = 18),
  ) +
  coord_cartesian(clip = "off")

(bar_rebel | bar_state) %>%
  add_global_label(
    Xlab = "Prevalence Level",
    size = 6,
    Xgap = 0.05,
    hjust = 0.3,
    vjust = 0.45
  ) &
  theme(plot.margin = margin(3, 3, 2, 0.75, "pt"))

ggsave("figures/summary plots/summary_plot_cs.pdf",
       width = 8,
       height = 4.5)
